package com.csj.cloud.user.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.csj.cloud.common.entity.User;
import com.csj.cloud.common.vo.ResultBean;
import com.csj.cloud.common.dao.UserMapper;
import com.csj.cloud.core.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author:ChengshaoJie
 * @Description:
 * @Date:Created in 下午 3:42 2019/11/19 0019
 */
@Slf4j
@Service
@com.alibaba.dubbo.config.annotation.Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private AmqpTemplate template;
    /**
     * 注册用户，赠送积分
     * */
    @Override
    public ResultBean reg(User u) {
        ResultBean resultBean = new ResultBean();
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("phone",u.getPhone());
        User user = this.selectOne(wrapper);
        if (user==null){
            resultBean.setErrorMsg("该手机号的用户已经存在");
            return resultBean;
        }
        boolean b = this.insert(u);
        if (b){
            resultBean.setMsg("注册成功");
            //触发赠送积分
            giveGift(u);
        }else {
            resultBean.setErrorMsg("注册失败");
        }
        return resultBean;
    }


    /**
     * 赠送积分
     * */
    private void giveGift(User u){
        template.convertAndSend("queue",u);

    }

}
